#include <iostream>

#include <nemesis.comm.Communicator.h>

#include <ldap.kclient.Menu.h>

using namespace std;
using namespace nemesis;
using namespace nemesis::comm;

const char* kclient::Menu::EventData = "ldap::kclient::Menu::Data";

kclient::Menu::Menu (Communicator* communicator) :
   Handler (communicator, Type::Custom, false),
   a_status (0)
{
   setfd (0);
}

void kclient::Menu::paint () const
   throw ()   
{
   cout << endl;
   cout << "1.- Establecer base (" << a_data.base << ")" << endl;
   cout << "2.- Establecer filter (" << a_data.filter << ")" << endl;
   cout << "3.- Establecer scope (" << a_data.scope << ")" << endl;
   cout << "4.- Limpiar base " << endl;
   cout << "5.- Limpiar filter " << endl;
   cout << "6.- Limpiar scope " << endl;
   cout << "s.- Enviar peticion" << endl;
   cout << endl << "q.- Salir" << endl;
   cout << "Elija una opcion: " << flush;
}

void kclient::Menu::apply ()
   throw (RuntimeException)
{
   char str [512];
   char option;

   while (!gets (str));
   option = str [0];

   switch (option) {
      case '1': 
         cout << endl << "Indique base: ";          
         while (!gets (str));
         a_data.base = str;
         paint ();
         break;
      case '2':
         cout << endl << "Indique filter: ";          
         while (!gets (str));
         a_data.filter = str;
         paint ();
         break;
      case '3':
         cout << endl << "Indique scope: ";          
         while (!gets (str));
         a_data.scope = str;
         paint ();
         break;
      case '4':
         a_data.base.clear ();
         paint ();
         break;
      case '5':
         a_data.filter.clear ();
         paint ();
         break;
      case '6':
         a_data.scope.clear ();
         paint ();
         break;
      case 's':
      case 'S':
         a_communicator->eventUser (EventData, &a_data);
         cout << "Peticion enviada ..." << endl;
         paint ();
         break;
      case 'q':
      case 'Q':
         a_communicator->requestStop ();
         break;
      default:
         cout << "Elija una opcion: " << flush;
         break;
   }   
}


